import {useState, useEffect} from 'react';
import {Select, message} from 'antd'
import {getCodeByEqType} from '@/services/equipment'

const SelectCodeByEqType = (props: any) => {
  const {onChange, types} = props
  const [list, setList] = useState([]);
  const [value, setValue] = useState(null);
  useEffect(() => {
    if (types != null) {
      getCodeByEqType({equipment_type: types}).then(res => {
        if (res.status === 200) {
          setList(res.data)
        } else {
          message.error(res.message).then()
        }
      })
    }
  }, [types])
  return (
    <Select
      showSearch
      filterOption={(input, option) => option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
      allowClear
      value={value}
      autoClearSearchValue
      onChange={(e) => {
        onChange(e);
        setValue(e);
      }}>
      {list.map(({equipment_code}) => (
        <Select.Option value={equipment_code}>{equipment_code}</Select.Option>
      ))}
    </Select>
  );
};

export default SelectCodeByEqType;
